package keer.httpinterface.http;

import keer.httpinterface.utils.StringUtils;

/**
 * 封装http请求信息的封装类
 * @see HttpMethod
 * @see HttpBody
 * @see HttpParams
 * @see Headers
 *
 * @author Keer
 * @since 1.0.0
 */
public class HttpRequest {
    private final HttpMethod method;
    //    private final Params params;
    private final HttpParams params;
    private final Headers headers;
    private final String url;
    private final HttpBody body;

    private HttpRequest(String url, HttpMethod method) {
        this(url, method, null);
    }

    private HttpRequest(String url, HttpMethod method, HttpBody body) {
        this.url = url;
        this.method = method;
        this.params = new HttpParams();
        this.headers = new Headers();
        this.body = body;
    }

    /**
     * 获取请求方法
     * @see HttpMethod
     * @return 请求方法
     */
    public HttpMethod getMethod() {
        return method;
    }

    /**
     * 获取请求参数
     * @see HttpParams
     * @return 请求参数
     */
    public HttpParams getParams() {
        return params;
    }

    /**
     * 获取请求头
     * @see Headers
     * @return 请求头
     */
    public Headers getHeaders() {
        return headers;
    }

    /**
     * 获取请求地址
     * @return 请求地址
     */
    public String getUrl() {
        return url;
    }

    /**
     * 获取请求体
     * @return 请求体
     */
    public HttpBody getBody() {
        return body;
    }

    /**
     * 设置请求头
     * @param key 请求头key
     * @param values 请求头value
     */
    public void setHeader(String key,String...values) {
        headers.put(key, values);
    }

    //
//    /**
//     * 创建一个GET请求
//     * @param url 请求地址
//     * @return GET请求
//     */
//    public static HttpRequest Get(String url) {
//        return new HttpRequest(url, HttpMethod.GET);
//    }
//
//    /**
//     * 创建一个GET请求
//     * @param url 请求地址
//     * @param params 请求参数
//     * @return GET请求
//     */
//    public static HttpRequest Get(String url, Params params) {
//        return new HttpRequest(url, HttpMethod.GET);
//    }
//
//    /**
//     * 创建一个POST请求
//     * @param url 请求地址
//     * @param body 请求体
//     * @return POST请求
//     */
//    public static HttpRequest Post(String url, HttpBody body) {
//        HttpRequest request = new HttpRequest(url, HttpMethod.POST, body);
//        request.getHeaders().setContentType(body.getContentType());
//        return request;
//    }
//
//    /**
//     * 创建一个PUT请求
//     * @param url 请求地址
//     * @param body 请求体
//     * @return PUT请求
//     */
//    public static HttpRequest Put(String url, HttpBody body) {
//        HttpRequest request = new HttpRequest(url, HttpMethod.PUT, body);
//        request.getHeaders().setContentType(body.getContentType());
//        return request;
//    }
//
//    /**
//     * 创建一个DELETE请求
//     * @param url 请求地址
//     * @return DELETE请求
//     */
//    public static HttpRequest Delete(String url) {
//        return new HttpRequest(url, HttpMethod.DELETE);
//    }
//
//    public static HttpRequest Head(String url) {
//        return new HttpRequest(url, HttpMethod.HEAD);
//    }
//
//    public static HttpRequest Options(String url) {
//        return new HttpRequest(url, HttpMethod.OPTIONS);
//    }

    /**
     * 创建一个不包含请求体的请求
     * @param url 请求地址
     * @param method 请求方法
     * @return 请求
     */
    public static HttpRequest create(String url, HttpMethod method) {
        return create(url, method, null);
    }

    /**
     * 创建一个请求
     * @param url 请求地址
     * @param method 请求方法
     * @param body 请求体
     * @return 请求
     */
    public static HttpRequest create(String url, HttpMethod method, HttpBody body) {
        HttpRequest request = new HttpRequest(url, method, body);
        if (body != null) {
            request.getHeaders().setContentType(body.getContentType());
        }
        return request;
    }

    /**
     * 以一个现有的{@link HttpRequest}对象为基础，创建一个新的请求
     * @param url 请求地址
     * @param request 源请求
     * @return 新的请求
     */
    public static HttpRequest create(String url, HttpRequest request) {
        HttpRequest req = create(url, request.getMethod(), request.getBody());
        req.getParams().putAll(request.getParams());
        req.getHeaders().putAll(request.getHeaders());
        return req;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Request ")
                .append(method.name())
                .append(" ")
                .append(url)
                .append("\n");
        String params = this.params.toString();
        if (!StringUtils.isBlank(params)) {
            builder.append("Parameters:")
                    .append(params)
                    .append("\n");
        }
        builder.append(headers);
        return builder.toString();
    }
}
